#include "SDL/SDL.h"
#include <windows.h>
#include "SDL/SDL_image.h"

#include "myaudio.h"

#include <cstdlib>
#include <iostream>
#include <string>

#include "Character.h"
#include "enemy.h"
#include "sdlstart.h"
#include "world.h"
using namespace std;


//Mix_Music *music = NULL;

//void handleKey(SDL_KeyboardEvent key);
//void musicDone();



int main( int argc, char* args[] )
{
    enemy dragon;
    enemy rabbit1;
    enemy rabbit2;
    enemy rabbit3;

    int draw_turn = 1;
    //////////////////////////////////////////////////

    //Make sure the program waits for a quit
    bool quit = false;

    sdlstart gamestart;

    //Initialize
    if( gamestart.init() == false )
    {
        return 1;
    }
    //Load the files
    if( gamestart.load_files() == false )
    {
        return 1;
    }

    int enemyanim = 0;

    //While the user hasn't quit


    int char1xval=0;
    int char2xval=0;
    int char3xval=0;
    bool enemy_action=false;

    myaudio jason("aargh.wav");
    myaudio bob("arrow.ogg");

    //myaudio jason("roar.mp3");
    //myaudio bob("arrow.mp3");

    //music = Mix_LoadMUS("roar.mp3");

    while( quit == false )
    {
        /////////////DO FIGHTING/////////////////////////////////////
    if (enemy_action==true)
    {
    int dragon_attack= dragon.update(rabbit1.getHealth(),rabbit2.getHealth(),rabbit3.getHealth());
    cout << "The dragon choose to attack " << dragon_attack << endl;

    if( dragon_attack > 300)
    {
        int damage = dragon_attack - 300;
        rabbit3.setDamage(damage);

    }
    else if( dragon_attack > 200)
    {
        int damage = dragon_attack - 200;
        rabbit2.setDamage(damage);

    }
    else if( dragon_attack > 100)
    {
        int damage = dragon_attack - 100;
        rabbit1.setDamage(damage);

    }
    else
    {
        cout << "All enemies dead " << endl;
    }
    char1xval = (300 - rabbit1.getHealth() );
    char2xval = (300 - rabbit2.getHealth() );
    char3xval = (300 - rabbit3.getHealth() );
    enemy_action=false;
    }//end enemy IF






        ////////////END FIGHTING/////////////////////////////////////


        ////////////Do DRAWING////////////////////////////////////////

        //Apply the surface to the screen
        gamestart.apply_surface( 0, 0, gamestart.background, gamestart.screen );

        //Apply the second surface to the screen
        gamestart.apply_surface( 75, 160, gamestart.char1, gamestart.screen );

        //Apply the second surface to the screen
        gamestart.apply_surface( 150, 200, gamestart.char2, gamestart.screen );

        //Apply the second surface to the screen
        gamestart.apply_surface( 225, 160, gamestart.char3, gamestart.screen );

        //Apply the second surface to the screen
        gamestart.apply_surface( 500, 5, gamestart.enemyhealthbar, gamestart.screen );
        gamestart.apply_surface( -char1xval, 5, gamestart.char1healthbar, gamestart.screen );
        gamestart.apply_surface( -char2xval, 25, gamestart.char2healthbar, gamestart.screen );
        gamestart.apply_surface( -char3xval, 45, gamestart.char3healthbar, gamestart.screen );



        if ( enemyanim == 0)
        {
            //Apply the third surface to the screen
            gamestart.apply_surface( 420, 40, gamestart.enemy, gamestart.screen );

            enemyanim = 1;
        }

        else
        {
            //Apply the third surface to the screen
            gamestart.apply_surface( 420, 40, gamestart.enemy2, gamestart.screen );

            enemyanim = 0;
        }

        //Apply the last surface to the screen - deleted
        //gamestart.apply_surface( 50, 320, gamestart.hud, gamestart.screen );

        //Drawing hud
        switch(draw_turn)
        {
            case 1:
                gamestart.apply_surface( 225, 260, gamestart.Wiz_Hud, gamestart.screen);
                break;
            case 2:

                gamestart.apply_surface( 315, 200, gamestart.Ehron_Hud, gamestart.screen);
                break;
            case 3:
                gamestart.apply_surface( 135, 200, gamestart.Gruff_Hud, gamestart.screen);
                break;
            default:
                break;
        };


        //Update the screen
        if( SDL_Flip( gamestart.screen ) == -1 )
        {
            return 1;
        }

       // Sleep(500);

        //While there's an event to handle
        SDL_Event event;
        while( SDL_PollEvent( &event ) )
        {
           //If the user has Xed out the window
          if(event.type == SDL_QUIT )
          {
               //Quit the program
               quit = true;
          }
          if(event.key.keysym.sym == SDLK_m)
          {
              enemy_action=true;
              jason.play();
          }
          if(event.key.keysym.sym == SDLK_c)
          {
             // enemy_action=true;
              bob.play();
          }
        }
        SDL_Delay(500);

    }

    //Free the surface and quit
   //   gamestart.clean_up();
     // Mix_CloseAudio();




    ///////////////////////////////////////////////////












    return 0;
}
/*
void musicDone() {
  Mix_HaltMusic();
  Mix_FreeMusic(music);
  music = NULL;
}*/
